package edu.zqu.instructionalMatters.project.controller;


import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.TeachingTaskVO;
import edu.zqu.instructionalMatters.project.entity.Semester;
import edu.zqu.instructionalMatters.project.entity.TeachingTask;
import edu.zqu.instructionalMatters.project.mapper.SemesterMapper;
import edu.zqu.instructionalMatters.project.mapper.TeachingTaskMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.InstructionClassService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教学任务书 前端控制器
 * </p>
 *
 * @author 何文东
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/teachingTask")
@Api(tags = "教学任务书")
public class TeachingTaskController {
    @Autowired
    private TeachingTaskMapper teachingTaskMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private SemesterMapper semesterMapper;
    @Autowired
    private CourseService courseService;
    @Autowired
    private InstructionClassService instructionClassService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取教学任务书列表")
    @RequestMapping(value = "/teachingTasks", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger teacherId,
                             BigInteger classCurriculumId,
                             BigInteger courseId,
                             BigInteger semesterId,
                             BigInteger instructionClassId
    ) {
        TeachingTask teachingTask1 = new TeachingTask()
                .setCollegeId(collegeId).setTeacherId(teacherId).setClassCurriculumId(classCurriculumId)
                .setCourseId(courseId).setSemesterId(semesterId).setInstructionClassId(instructionClassId);             //合成TeachingTask对象
        List<TeachingTask> list1 = teachingTaskMapper.findAll(teachingTask1);                                           //获取 TeachingTask 集合
        List<TeachingTaskVO> list = new ArrayList<>();                                                                  //初始化 VO集合
        for (TeachingTask teachingTask : list1) {                                                                        //循环遍历
            TeachingTaskVO teachingTaskVO = new TeachingTaskVO();                                                       //初始化 VO
            BeanUtils.copyProperties(teachingTask, teachingTaskVO);                                                      //对象复制赋值
            String collegeName = collegeService.getCollegeName(teachingTask.getCollegeId());                            //获取学校名称
            teachingTaskVO.setCollegeName(collegeName);                                                                 //设置学校名称
            Semester semester = semesterMapper.selectById(teachingTask.getSemesterId());                                //获取学期对象
            teachingTaskVO.setSemesterName(null);                                                                       //设置学期名称null
            if (semester != null) {
                teachingTaskVO.setSemesterName(semester.getName());                                                     //设置学期名称
            }
            String courseName = courseService.getCourseName(teachingTask.getCourseId());                                //获取课程名称
            teachingTaskVO.setCourseName(courseName);                                                                   //设置课程名称
            String instructionClassName = instructionClassService.getInstructionClassName(teachingTask.getInstructionClassId());//授课班级名称
            teachingTaskVO.setInstructionClassName(instructionClassName);                                               //设置授课班级名称
            String teacherName = userService.getUserName(teachingTask.getTeacherId());                                  //获取教师名称
            teachingTaskVO.setTeacherName(teacherName);                                                                 //设置教师名称
            list.add(teachingTaskVO);                                                                                   //加入 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取教学任务书信息", response = TeachingTaskVO.class)
    @GetMapping(value = "/teachingTask/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        TeachingTask teachingTask = teachingTaskMapper.selectById(id);                                       //查询对象数据
        TeachingTaskVO teachingTaskVO = new TeachingTaskVO();                                                //初始化VO
        BeanUtils.copyProperties(teachingTask, teachingTaskVO);                                               //对象复制赋值
        String collegeName = collegeService.getCollegeName(teachingTask.getCollegeId());                     //获取学校名称
        teachingTaskVO.setCollegeName(collegeName);                                                          //设置学校名称
        Semester semester = semesterMapper.selectById(teachingTask.getSemesterId());                         //获取学期对象
        teachingTaskVO.setSemesterName(null);                                                                //设置学期名称null
        if (semester != null) {
            teachingTaskVO.setSemesterName(semester.getName());                                              //设置学期名称
        }
        String courseName = courseService.getCourseName(teachingTask.getCourseId());                         //获取课程名称
        teachingTaskVO.setCourseName(courseName);                                                            //设置课程名称
        String instructionClassName = instructionClassService.getInstructionClassName(teachingTask.getInstructionClassId());//授课班级名称
        teachingTaskVO.setInstructionClassName(instructionClassName);                                        //设置授课班级名称
        String teacherName = userService.getUserName(teachingTask.getTeacherId());                           //获取教师名称
        teachingTaskVO.setTeacherName(teacherName);                                                          //设置教师名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", teachingTaskVO);                           //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/teachingTask", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger teacherId,
                         BigInteger classCurriculumId,
                         BigInteger courseId,
                         BigInteger semesterId,
                         BigInteger instructionClassId,
                         BigInteger byWho) {
        TeachingTask teachingTask = new TeachingTask();                            //初始化
        teachingTask.setCollegeId(collegeId);                                      //设置学校id
        teachingTask.setTeacherId(teacherId);                                      //设置教师id
        teachingTask.setClassCurriculumId(classCurriculumId);                      //设置班级课程id
        teachingTask.setCourseId(courseId);                                        //设置课程id
        teachingTask.setSemesterId(semesterId);                                    //设置学期id
        teachingTask.setInstructionClassId(instructionClassId);                    //设置授课班级id
        teachingTask.setByWho(byWho);                                              //设置操作者
        teachingTaskMapper.insert(teachingTask);                                   //执行插入
        return ZQUResult.success();                                                //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/teachingTask/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "classCurriculumId", value = "班级课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger teacherId,
                            BigInteger classCurriculumId,
                            BigInteger courseId,
                            BigInteger semesterId,
                            BigInteger instructionClassId,
                            BigInteger byWho) {
        TeachingTask teachingTask = teachingTaskMapper.selectById(id);            //先查出旧纪录
        teachingTask.setByWho(byWho);                                              //修改操作者
        teachingTaskMapper.updateById(teachingTask);                               //更新操作者
        teachingTaskMapper.deleteById(id);                                         //逻辑删除
        teachingTask = new TeachingTask();                                         //初始化新记录
        teachingTask.setCollegeId(collegeId);                                      //设置学校id
        teachingTask.setTeacherId(teacherId);                                      //设置教师id
        teachingTask.setClassCurriculumId(classCurriculumId);                      //设置班级课程id
        teachingTask.setCourseId(courseId);                                        //设置课程id
        teachingTask.setSemesterId(semesterId);                                    //设置学期id
        teachingTask.setInstructionClassId(instructionClassId);                    //设置授课班级id
        teachingTask.setByWho(byWho);                                              //设置操作者
        teachingTask.setLastVersion(id);                                           //设置上一版本
        teachingTaskMapper.insert(teachingTask);                                   //执行插入
        return ZQUResult.success();                                                //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/teachingTask/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        TeachingTask teachingTask = teachingTaskMapper.selectById(id);            //先查出旧纪录
        teachingTask.setByWho(byWho);                                              //修改操作者
        teachingTaskMapper.updateById(teachingTask);                               //更新操作者
        teachingTaskMapper.deleteById(id);                                         //逻辑删除
        return ZQUResult.success();                                                //操作成功返回
    }
}

